package com.oa.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.oa.bean.bazaar.Inten;
import com.oa.bean.work.Emp;
import com.oa.dao.BazaarDAO;

public class BazaarDAOImpl implements BazaarDAO {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Override
	public Emp zhaoAdminAdd(Emp emp) {//添加招生管理员
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(emp);
		session.getTransaction().commit();
		return emp;
	}

	@Override
	public Emp zhaoAdminUpdate(Emp emp) {//修改招生管理员
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.saveOrUpdate(emp);
		session.getTransaction().commit();
		return emp;
	}

	@Override
	public void zhaoAdminDong(Emp emp) {//冻结招生管理员
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Query query = session.createQuery("update Emp e set e.estatus=? where e.eid=?");
		query.setInteger(0, emp.getEstatus());
		query.setInteger(1, emp.getEid());
		query.executeUpdate();
		session.getTransaction().commit();
		
	}

	@Override
	public List<Emp> zhaoAdminQueryPager(int page, int pageSize) {//分页查询招生管理员
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Emp e where e.estatus like '"+1+"%'"); 
		query.setFirstResult((page - 1) * pageSize);// 设置从哪里开始查询，0开始第一条
		query.setMaxResults(pageSize);// 设置查询的total(数量)
		@SuppressWarnings("unchecked")
		List<Emp> list = query.list();
		return list;
	}

	@Override
	public void save(Inten t) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			session.save(t);
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
	}

	@Override
	public void delete(Inten t) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			session.delete(t);
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
	}

	@Override
	public Inten update(Inten t) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			session.update(t);
			tr.commit();
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

	@Override
	public List<Inten> queryAll() {
		return null;
	}

	@Override
	public Inten queryById(Integer id) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			Inten i = (Inten) session.get(Inten.class, id);
			tr.commit();
			return i;
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> intenQueryPager(Integer page, Integer pageSize) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			Criteria c = session.createCriteria("from Inten");
			c.setFirstResult((page - 1) * pageSize);
			c.setMaxResults(pageSize);
			List<Object> intens = c.list();
			tr.commit();
			return intens;
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> intenPagerByName(Integer page, Integer pageSize, String name) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			Criteria c = session.createCriteria("from Inten");
			c.setFirstResult((page - 1) * pageSize);
			c.setMaxResults(pageSize);
			c.add(Restrictions.like("intenname", "%" + name + "%"));
			List<Object> intens = c.list();
			tr.commit();
			return intens;
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> intenPagerByAge(Integer page, Integer pageSize, Integer age) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			Criteria c = session.createCriteria("from Inten");
			c.setFirstResult((page - 1) * pageSize);
			c.setMaxResults(pageSize);
			c.add(Restrictions.eq("intenage", age));
			List<Object> intens = c.list();
			tr.commit();
			return intens;
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

	@Override
	public List<Object> intenPagerByIntroduce(Integer page, Integer pageSize, String introduceName) {
		// 没有介绍老师的字段，不能按介绍老师查询
		return null;
	}

	@Override
	public List<Object> intenPagerByEducation(Integer page, Integer pageSize, String educationName) {
		// 没有学历的字段，不能按学历查询
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> intenPagerByCollege(Integer page, Integer pageSize, String collegeName) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			Criteria c = session.createCriteria("from Inten");
			c.setFirstResult((page - 1) * pageSize);
			c.setMaxResults(pageSize);
			c.add(Restrictions.like("intensch", "%" + collegeName + "%"));
			List<Object> intens = c.list();
			tr.commit();
			return intens;
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Object> intenPagerByBirthplace(Integer page, Integer pageSize, String birthplaceName) {
		Session session = sessionFactory.openSession();
		Transaction tr = session.beginTransaction();
		try {
			Criteria c = session.createCriteria("from Inten");
			c.setFirstResult((page - 1) * pageSize);
			c.setMaxResults(pageSize);
			c.add(Restrictions.like("intenaddr", "%" + birthplaceName + "%"));
			List<Object> intens = c.list();
			tr.commit();
			return intens;
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		} finally {
			session.close();
		}
		return null;
	}

}
